/*
2011-03-29:by lisiheng
当JointCallExecution 字段原来为未选取而更新为选取时，查询有没有对应Joint call，
没有则查询今天该MSR的CALLPLAN对应的Joint call的所有人，给目标说有人都添加一条对
应该记录的Joint call，如果没有所有人，则为当前MSR的MANAGER添加JOINT CALL；
*/
trigger CreateCallPlanForManager on CallPlan__c (after update) {
 
    list<CallPlan__c> CallPlanList=new list<CallPlan__c>(); 
    list<date> CallDateList=new list<date>();
    list<string> ownerList=new list<string>();
    list<id> ManagerIdList=new list<id>();
    for(callplan__c t:trigger.new){
        if(trigger.oldmap.get(t.id).JointCallExecution__c != t.JointCallExecution__c && t.JointCallExecution__c && (t.PlanType__c == '计划外')){
            CallPlanList.add(t);
            calldatelist.add(t.CallPlanDate__c);
            ownerList.add(string.valueof(t.ownerid__c).substring(0,15));
            ManagerIdList.add(t.managerid__c);
        }
    }
    if(callPlanList.size()>0){
        list<JointCall__c> jointCallList=[select id,callplan__c,CallPlanOwner__c,MonthlyJointCall__c,owner__c,CallDate__c from JointCall__c where CallPlanOwner__c in : ownerList and CallDate__c in :CallDateList];
        set<id> JointCallPlanIdSet=new set<id>();
        list<callplan__c> noJointCallCallplanList=new list<callPlan__c>();
        Map<id,id> MonthlyJointCallMap=new Map<id,id>();
        list<integer> monthList=new list<integer>();
        list<integer> yearList=new list<integer>(); 
      //  system.assert(false,jointcalllist.size()+'        '+jointCallList[0].CallPlanOwner__c +'    '+ownerlist[0]);
        for(JointCall__c jc:jointcallList){
            JointCallPlanIdSet.add(jc.callplan__c);
            MonthlyJointCallMap.put(jc.owner__c,jc.MonthlyJointCall__c);
        }
        for(CallPlan__c CallPlan:CallPlanList){
            if(!JointCallPlanIdSet.contains(callplan.id)){
                noJointCallCallplanList.add(callplan);
                Monthlist.add(callplan.callplandate__c.month());
                yearlist.add(callplan.callplandate__c.year());
            }
        }
        
        if(noJointCallCallPlanList.size()>0){
            list<jointcall__c> JointCallUpsertList=new list<JointCall__c>();
            set<id> managerId=new set<id>();
            list<Monthly_Joint_Call__c> managerMonthlyJointCallList=[select id,ownerid,MonthFormula__c,YearFormula__c from Monthly_Joint_Call__c where ownerid in: manageridlist and MonthFormula__c in:monthlist and YearFormula__c in: yearlist];
            //将monthly Joint Call 放入一个map key为owner+年月，value为MJC  ID；
            map<string,id> managerMonthlyJointCallMap=new map<string,id>();
            
            for(Monthly_joint_call__c mjc:managerMonthlyJointCallList){
                managerMonthlyJointCallMap.put(''+string.valueof(mjc.ownerid).substring(0,15)+mjc.YearFormula__c+mjc.MonthFormula__c,mjc.id);
            }
            for(CallPlan__c cp:noJointCallCallPlanList){
                managerid.clear();
                for(jointCall__c jc:jointCallList){
                    if(cp.CallPlanDate__c == jc.CallDate__c && cp.ownerid__c==jc.CallPlanOwner__c){
                        managerid.add(jc.owner__c);
                    }
                }
                if(managerid.size()>0){
                    //给所有找出的经理添加一条jointCall
                    for(id m:managerid){
                        jointcall__c jc=new jointcall__c();
                        jc.CallPlan__c=cp.id;
                        jc.name = cp.name;
                        jc.MonthlyJointCall__c=monthlyJointCallMap.get(m);
                        jc.Owner__c=m;
                        jc.UniqueId__c=string.valueof(cp.id).substring(0,15)+string.valueof(m).substring(0,15);
                        jointCallUpsertList.add(jc);
                    }
                }else{
                    //揪出直接经理给他塞条jointcall！
                    if(!managerMonthlyJointCallMap.containskey(''+cp.managerid__c+cp.callplandate__c.year()+cp.callplandate__c.month())){
                        Monthly_Joint_Call__c newMonthlyJointCall=new Monthly_Joint_Call__c(
                            ownerid=cp.managerid__c,
                            name=cp.manager_formula__c + ' '+cp.callplandate__c.year() +'年'+cp.callplandate__c.month()+'月共同拜访计划',
                            Year__c=string.valueof(cp.callplandate__c.year()),
                            Month__c=string.valueof(cp.callplandate__c.month())
                        );
                        insert newMonthlyJointCall;
                        managerMonthlyJointCallMap.put(''+cp.managerid__c+cp.callplandate__c.year()+cp.callplandate__c.month(),newMonthlyJointCall.id);
                    }
                    jointcall__c jc=new jointcall__c();
                    jc.callplan__c=cp.id;
                    jc.name = cp.name;
                    jc.MonthlyJointCall__c=managerMonthlyJointCallMap.get(''+cp.managerid__c+cp.callplandate__c.year()+cp.callplandate__c.month());
                    jc.Owner__c=cp.managerid__c;
                    jc.UniqueId__c=string.valueof(cp.id).substring(0,15)+string.valueof(cp.managerid__c).substring(0,15);
                    jointCallUpsertList.add(jc);
                }
            }
            upsert JointCallUpsertList UniqueId__c;
            
        }
        
        
    }
 
        
}